home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / elk-2_0.lha / elk-2.0 / src / stab.hp9k300.c < prev    next >
C/C++ Source or Header  |  1992-10-21  |  3KB  |  110 lines

  1. #include <a.out.h>
  2. #include <sys/types.h>
  3.  
  4. extern char *malloc(), *realloc();
  5.  
  6. /* On the HP9000 an nlist entry contains a fixed length
  7.  * part consisting of the symbol information, plus a variable
  8.  * length part, the name without a '\0' terminator. 
  9.  * We don't know how much space to allocate for the names 
  10.  * until we have read them all.
  11.  * The solution here is to save all the names on the fly
  12.  * in a table that is grown in units of STRING_BLOCK bytes,
  13.  * using realloc to expand it on demand.
  14.  */
  15.  
  16. #define STRING_BLOCK 8192
  17.  
  18. char *Safe_Realloc (ptr, size) char *ptr; int size; {
  19.     char *ret;
  20.  
  21.     ret = ptr ? (char *)realloc( ptr, size ) : (char *)malloc( size );
  22.     if (ret == 0)
  23.     Primitive_Error ("not enough memory to allocate ~s bytes",
  24.         Make_Fixnum (size));
  25.     return ret;
  26. }
  27.  
  28. SYMTAB *Snarf_Symbols (f, ep) FILE *f; struct exec *ep; {
  29.     SYMTAB       *tab;
  30.     register SYM *sp;
  31.     register SYM **nextp;
  32.     int          strsiz = 0; /* running total length of names read, */
  33.                  /*   each '\0' terminated */ 
  34.     int          nread = 0;  /* running total of bytes read from symbol table */
  35.     int          max = 0;    /* current maximum size of name table */
  36.     char         *names = 0; /* the name table */
  37.     struct nlist_ nl;
  38.  
  39.     tab = (SYMTAB *)Safe_Malloc (sizeof (SYMTAB));
  40.     tab->first = 0;
  41.     tab->strings = 0;
  42.     nextp = &tab->first;
  43.  
  44.     (void)fseek (f, (long)LESYM_OFFSET(*ep), 0);
  45.  
  46.     while (nread < ep->a_lesyms) {
  47.     if (fread ((char *)&nl, sizeof (nl), 1, f) != 1) {
  48.         Free_Symbols (tab);
  49.         (void)fclose (f);
  50.         Primitive_Error ("corrupt symbol table in object file");
  51.     }
  52.  
  53.         nread += sizeof( nl );
  54.  
  55.     if (nl.n_length == 0) {
  56.             continue;
  57.         }
  58.         else if (nl.n_length + strsiz + 1 > max) {
  59.             max += STRING_BLOCK;
  60.             names = Safe_Realloc( names, max );
  61.         }
  62.  
  63.         if (fread( names + strsiz, 1, nl.n_length, f ) != nl.n_length) {
  64.         Free_Symbols (tab);
  65.         (void)fclose (f);
  66.         Primitive_Error ("corrupt symbol table in object file");
  67.     }
  68.         else {
  69.             nread += nl.n_length; 
  70.             names[ strsiz + nl.n_length ] = '\0';
  71.         }
  72.     if ((nl.n_type & N_TYPE) != N_TEXT) {
  73.         strsiz += nl.n_length +1;
  74.         continue;
  75.     }
  76.     sp = (SYM *)Safe_Malloc (sizeof (SYM));
  77.     sp->name = (char *)strsiz;
  78.         strsiz += (nl.n_length + 1);
  79.     sp->value = nl.n_value;
  80.     *nextp = sp;
  81.     nextp = &sp->next;
  82.     *nextp = 0;
  83.     }
  84.  
  85.     tab->strings = names;
  86.  
  87.     for (sp = tab->first; sp; sp = sp->next)
  88.     sp->name += (unsigned)names;
  89.  
  90.     return tab;
  91. }
  92.  
  93. #ifdef INIT_OBJECTS
  94. SYMTAB *Open_File_And_Snarf_Symbols (name) char *name; {
  95.     struct exec hdr;
  96.     FILE *f;
  97.     SYMTAB *tab;
  98.  
  99.     if ((f = fopen (name, "r")) == NULL)
  100.     Primitive_Error ("can't open a.out file");
  101.     if (fread ((char *)&hdr, sizeof hdr, 1, f) != 1) {
  102.     (void)fclose (f);
  103.     Primitive_Error ("can't read a.out header");
  104.     }
  105.     tab = Snarf_Symbols (f, &hdr);
  106.     (void)fclose (f);
  107.     return tab;
  108. }
  109. #endif /* INIT_OBJECTS */
  110.